import { reqGetGoodsInfo, reqAddOrUpdateShopcart } from '@/api'
import { getUUID } from '@/utils/uuidToken'
export default {

  namespaced: true,

  state: {
    // 服务器响应回来的数据
    goodsInfo: {},
    uuidToken: getUUID()

  },

  actions: {
    async getGoodsInfo({ commit }, skuId) {
      const result = await reqGetGoodsInfo(skuId)
      if (result.code === 200) {
        commit('GETGOODSINFO', result.data)
      }
    },
    async addOrUpdateShopcart(context, { skuId, skuNum }) {
      const result = await reqAddOrUpdateShopcart(skuId, skuNum)
      if (result.code === 200) {
        return 'ok'
      } else {
        return Promise.reject(new Error('faile'))
      }
    }
  },

  mutations: {

    GETGOODSINFO(state, goodsInfo) {
      state.goodsInfo = goodsInfo
    }
  },

  getters: {

    categoryView(state) {
      return state.goodsInfo.categoryView || {}
    },
    skuInfo(state) {
      return state.goodsInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
      return state.goodsInfo.spuSaleAttrList || []
    }
  }
}
